﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Linq;

namespace Test_API
{
    class GroupWorkSpaceTest : GenericModuleTest
    {
        protected override string moduleName
        {
            get
            {
                return "Group Workspace Test";
            }
        }

        private string respondToRequestsMethod;
        private string respondToInvitationsMethod;
        private string inviteMembersMethod;
        private string viewAvailableGroupsMethod;
        public GroupWorkSpaceTest(string accessToken, string cid)
            : base(accessToken)
        {
            this.viewAllMethod = "viewMyGroupWorkspace";
            this.viewAvailableGroupsMethod = "viewAvailableGroupsInGroupWorkspace";
            this.inviteMembersMethod = "gwsInviteMemberInGroup";
            this.respondToInvitationsMethod = "gwsRespondToInvitation";
            this.respondToRequestsMethod = "gwsRespondToRequest";
            this.deleteMethod = "gwsLeaveGroup";
            this.cid = cid;
        }

        protected override string genericQueryStringGenerate(int testId)
        {
            string queryString = new Dictionary<string, string>() { { "cid", this.cid }, { "accesstoken", this.accesstoken }, { "groupId", testId.ToString() }, { "itemId", testId.ToString() } }.buildQueryString();

            return queryString;
        }
        protected override Dictionary<string, object> genericPostDataSetGenerate()
        {
            Dictionary<string, object> dataSet = new Dictionary<string, object>();
            {
                dataSet.Add("emails", "abcd@example.com; xy" + ExtenstionMethods.randomNumber(5000, 9999).ToString() + "@rwth-aachen.de");
                dataSet.Add("systemGeneratedAlias", Library.getWord());
                dataSet.Add("comment", Library.getTitle());
            }

            return dataSet;
        }

        private List<LogEntry> viewAvailableGroups()
        {
            string temp = this.viewAllMethod;
            List<int> tempIds = this.validItemIDs;
            this.viewAllMethod = this.viewAvailableGroupsMethod;
            List<LogEntry> entries = base.viewAll("groupId");
            this.viewAllMethod = temp;
            this.validItemIDs = tempIds;
            return entries;
        }

        private List<LogEntry> respondToAction(string method, string whichParam)
        {
            List<LogEntry> entries = base.viewAll("groupId");
            if (entries[0].verdict == false)
            {
                return entries;
            }

            JObject receivedJson = JObject.Parse(entries[0].receivedData);
            LogEntry t1 = new LogEntry(this.moduleName, true);
            if (receivedJson[whichParam] == null || receivedJson[whichParam].Count() == 0)
            {
                t1.methodName = method;
                t1.verdict = true;
                t1.elapsedTime = 0;
                t1.inputs = "No itemid for " + whichParam;
                t1.receivedData = null;
                entries[0] = t1;
                return entries;
            }

            for (int i = 0; i < receivedJson[whichParam].Count(); ++i)
            {
                var itemId = (int)receivedJson[whichParam][i]["itemId"];

                string queryString = this.genericQueryStringGenerate(itemId) + "action=" + ExtenstionMethods.randomNumber(1, 2) + "&";
                HTTP.execute(HTTP.RequestType.GET, this.rootUrl, method, queryString, null, ref t1);
                break; //Only 1 action will be sent for testing
            }
            entries.Clear();
            entries.Add(t1);
            return entries;
        }

        private List<LogEntry> respondToRequest()
        {
            return this.respondToAction(this.respondToRequestsMethod, "requestFromOtherUsers");
        }

        private List<LogEntry> respondToInvitation()
        {
            return this.respondToAction(this.respondToInvitationsMethod, "invitationFromOtherUsers");
        }


        private List<LogEntry> inviteMembers()
        {
            List<LogEntry> entries = new List<LogEntry>();
            LogEntry t1 = new LogEntry(this.moduleName, true);

            HTTP.execute(HTTP.RequestType.POST, this.rootUrl, this.inviteMembersMethod, this.genericQueryStringGenerate(), this.genericPostDataSetGenerate(), ref t1);
            
            entries.Add(t1);
            return entries;
        }

        public new List<LogEntry> start()
        {
            Console.WriteLine("Group Workspace API call begin\n============");
            List<LogEntry> entries = new List<LogEntry>();

            entries.AddRange(this.viewAll("groupId"));
            entries.AddRange(this.viewAvailableGroups());
            entries.AddRange(this.inviteMembers());
            entries.AddRange(this.respondToInvitation());
            entries.AddRange(this.respondToRequest());
            entries.AddRange(this.delete());
            return entries;
        }
    }
}
